package com.example.litingting.adapterdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ListView lv1 ;
    private ListView lv2;
    private String[] date ;
    private List<Map<String,Object>> data1;
    public  void init(){
        data1 = new ArrayList<Map<String,Object>>();
        Map<String,Object> map1 = new HashMap<String, Object>();
        Map<String,Object> map2 = new HashMap<String, Object>();
        Map<String,Object> map3 = new HashMap<String, Object>();
        map1.put("snap",R.drawable.a1);
        map1.put("title","疯狂动物城");
        map1.put("content","狐尼克");
        map2.put("snap",R.drawable.a2);
        map2.put("title","疯狂动物城");
        map2.put("content","树懒");
        map3.put("snap",R.drawable.a3);
        map3.put("title","疯狂动物城");
        map3.put("content","狐尼克和兔朱迪");
        data1.add(map1);data1.add(map2);data1.add(map3);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv1 =(ListView)findViewById(R.id.lv1);
        date = new String[]{"hi","hello"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
                date);
        lv1.setAdapter(adapter);
        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("li", date[position]);
            }
        });
        lv2 = (ListView)findViewById(R.id.lv2);
        init();
        SimpleAdapter adapter1 = new SimpleAdapter(this,data1,R.layout.news_layout,
                new String[]{"snap","title","content"},new int[]{R.id.snap,R.id.title,R.id.content});
        lv2.setAdapter(adapter1);
        lv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("li",data1.get(position).get("title").toString()+".....................");
            }
        });
    }
}
